<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\CreditModel;
use think\Request;

class Credit extends Base
{
    //获取所有积分物品信息数据
    public function getAllCreditInfo(Request $request)
    {

//        parent::initialize();

//        $user_id=UserModel::getUseridByToken($request->param(('token'))); //获取用户id

//        dump($page);
        $db=new CreditModel();

        $info = $db->select();

        $total=$db->count('credit_item_id');

        return json(['code'=>1,'data'=>$info,'total'=>$total]);

    }

    //根据积分物品id删除积分物品
    public function deleteCreditInfo(Request $request){

        $credit_item_ids=$request->param('credit_item_id');

//批量删除
        if(is_array($credit_item_ids)){

            foreach ($credit_item_ids as $ids){
                $credit_item= CreditModel::find($ids);
                $credit_item_img=$credit_item['credit_item_img'];
                $college_img_url= FILEDIR.'credit_img/'.$credit_item_img;
                if($credit_item->delete()){
                    if($credit_item_img != 'default_credit_img.jpg'){
                        if( unlink($college_img_url)) {}
                    }
                }
                else{
                    return json(array(
                        'code' => 0
                    ));
                }
            }
            return json(array(
                'code' => 1
            ));


        }
        else{
            $credit_item= CreditModel::find($credit_item_ids);
            $credit_item_img=$credit_item['credit_item_img'];
            $college_img_url= FILEDIR.'credit_img/'.$credit_item_img;
            if($credit_item->delete()){
                if($credit_item_img != 'default_credit_img.jpg'){
                    if( unlink($college_img_url))
                    {
                        //返回消息
                        return json(array(
                            'code' => 1
                        ));
                    }
                }
                else
                {
                    return json(array(
                        'code' => 1
                    ));
                }

            }

            return json(array(
                'code' => 0
            ));
        }


    }

    //添加积分物品信息
    public function addCreditInfo(Request $request){


        $credit_item_name=$_POST['credit_item_name'];
        $credit_item_info=$_POST['credit_item_info'];
        $credit_item_req_credit=$_POST['credit_item_req_credit'];
        $credit_item_stock=$_POST['credit_item_stock'];
        $credit_item_type=$_POST['credit_item_type'];

//        没有上传图片
        if($_POST['if_img']==0){
            $credit_save_img='default_credit_img.jpg';
        }
        else{
            //上传图片文件
            $credit_item_img=$_FILES['credit_item_img'];
            //将图片存入目录
            $dir_img = FILEDIR.'credit_img/';

            if(!is_dir($dir_img))
            {
                mkdir($dir_img, 0777, true);
                chmod($dir_img, 0777); // 修改文件模式，所有人都有读、写、执行权限
            }
            else{
                chmod($dir_img, 0777); // 修改文件模式，所有人都有读、写、执行权限
            }

            // 接收图片文件信息
            $img_name = $credit_item_img['name'];

            $img_tmp_name = $credit_item_img['tmp_name']; //储存的临时文件名，一般是系统默认
            $img_ext = pathinfo($img_name, PATHINFO_EXTENSION); // 提取上传文件的扩展名
            // 得到唯一的文件名，防止因文件名相同而产生覆盖
            $credit_save_img = md5(uniqid('',true)).".$img_ext"; // md5加密，uniqid产生唯一id，microtime作前缀
            // 目标存放文件地址
            $img_destination = $dir_img."/".$credit_save_img;
            // 当文件上传成功时，存入临时文件夹，服务器开始判断
            if (@move_uploaded_file($img_tmp_name, $img_destination))
            {
//            @错误抑制符，不让用户看到警告
            }
            else {
                //echo "文件".$img_tmp_name."上传失败！";
                //返回消息
                return json(array(
                    'code' => 0
                ));
            }
            chmod($img_destination, 0777); // 修改文件模式，所有人都有读、写、执行权限
        }

        $data = [
            'credit_item_name' => $credit_item_name,
            'credit_item_info' => $credit_item_info,
            'credit_item_req_credit' => $credit_item_req_credit,
            'credit_item_stock' => $credit_item_stock,
            'credit_item_type' => $credit_item_type,
            'credit_item_img' => $credit_save_img,


        ];

        $db=new CreditModel();

        if ($db->save($data)) {
            //返回消息 上传成功
            return json(array(
                'code' => 1
            ));
        }
        else {
            //返回消息
            return json(array(
                'code' => 0
            ));
        }


    }

//    修改积分物品信息
    public function updateCreditInfo(Request $request){

        $EditForm=$request->param('EditForm');

        $credit_item_id=$request->param('credit_item_id');

        $credit_item=CreditModel::find($credit_item_id);

        if($credit_item->save($EditForm)){

            return json(['code'=>1]);
        }

        return json(['code'=>0]);

    }


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
